package top.heliming.youyou.user.controller;

import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import top.heliming.youyou.pojo.User;
import top.heliming.youyou.user.service.UserService;

/**
 * description: //TODO
 *
 * @author: heliming
 * @date:2019/06/08 下午 12:20
 */
@RestController
public class UserController {

  @Autowired
  private UserService userService;

  /**
   * 校验数据是否可用
   * @param data
   * @param type
   * @return
   */
  @GetMapping("check/{data}/{type}")
  public ResponseEntity<Boolean> checkUserData(@PathVariable("data") String data, @PathVariable(value = "type") Integer type) {
    Boolean boo = this.userService.checkData(data, type);
    if (boo == null) {
      return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
    }
    return ResponseEntity.ok(boo);
  }

  /**
   * 发送手机验证码
   * @param phone
   * @return
   */
  @PostMapping("code")
  public ResponseEntity<Void> sendVerifyCode(String phone) {
    Boolean boo = this.userService.sendVerifyCode(phone);
    if (boo == null || !boo) {
      return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
    }
    return new ResponseEntity<>(HttpStatus.CREATED);
  }

  /**
   * 注册
   * @param user
   * @param code
   * @return
   */
  @PostMapping("register")
  public ResponseEntity<Void> register(@Valid User user,BindingResult result, @RequestParam("code") String code) {
    if(result.hasErrors()){
      for (ObjectError error : result.getAllErrors()) {
        DefaultMessageSourceResolvable feild = (DefaultMessageSourceResolvable) error.getArguments()[0];
        System.out.println("参数["+feild.getDefaultMessage()+"]:"+error.getDefaultMessage());
      }
      return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
    }
    Boolean boo = this.userService.register(user, code);
    if (boo == null || !boo) {
      return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
    }
    return new ResponseEntity<>(HttpStatus.CREATED);
  }

  /**
   * 根据用户名和密码查询用户
   * @param username
   * @param password
   * @return
   */
  @GetMapping("query")
  public ResponseEntity<User> queryUser(
      @RequestParam("username") String username,
      @RequestParam("password") String password
  ) {
    User user = this.userService.queryUser(username, password);
    if (user == null) {
      return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
    }
    return ResponseEntity.ok(user);
  }
}
